#include "xToolManager.h"



namespace XE	
{


	xBaseToolManager::xBaseToolManager(xSceneManager* pSceneManager)
		:m_pCamera(NULL),m_pSceneManager(pSceneManager),m_pActiveTool(NULL)
	{	
		xBaseTool* pTool =new xMoveTool(this);
		pTool->InitTool();
		RegisterTool(pTool);
		SetActiveTool(pTool->GetName());


		pTool =new xRotationTool(this);
		pTool->InitTool();
		RegisterTool(pTool);

		pTool = new xSelectTool(this);
		pTool->InitTool();
		RegisterTool(pTool);
		SetActiveTool(pTool->GetName());

		
	}

	xBaseToolManager::~xBaseToolManager()
	{
		DestoryAllTools();

	}



	bool xBaseToolManager::RegisterTool(xBaseTool* ptool) 
	{
		if(ptool==NULL)
			return false;
	    if(FindTool(ptool->GetName()))
		{
			xLogMessager::getSingleton().logMessage("BaseToolManager RegisterTool Fail tool name is"+ptool->GetName());
			return false;
		}
		m_ToolVector.push_back(ptool);
		return true;
	}

	bool  xBaseToolManager::UnRegisterTool(xBaseTool* ptool)
	{

		xBaseToolVector::iterator it = std::find(m_ToolVector.begin(),m_ToolVector.end(),ptool);
		if(it!=m_ToolVector.end())
		{
			m_ToolVector.erase(it);
			return true;
		}
		return false;
	}

	bool  xBaseToolManager::UnRegisterTool(const xString& name)
	{
		xBaseToolVector::iterator it =m_ToolVector.begin();
		for(;it!=m_ToolVector.end();++it)
		{
			if((*it)->GetName()==name)
			{
				m_ToolVector.erase(it);
				return true;
			}
		}

		return false;
	}

	void  xBaseToolManager::DestoryAllTools()
	{
		xBaseToolVector::iterator it =m_ToolVector.begin();
		for(;it!=m_ToolVector.end();++it)
		{
			 delete *it;
		}
		m_ToolVector.clear();

	}

	xBaseTool* xBaseToolManager::FindTool(const xString& name)
	{

		xBaseToolVector::iterator it =m_ToolVector.begin();
		for(;it!=m_ToolVector.end();++it)
		{
			if((*it)->GetName()==name)
			{
				return *it;
			}
		}
		return NULL;
	}

	bool  xBaseToolManager::SetActiveTool(const xString& name)
	{
		xBaseTool* ptool = FindTool(name);
		if(ptool!=NULL)
		{
			if(m_pActiveTool!=NULL)
				m_pActiveTool->DesActive();

			m_pActiveTool = ptool;
			if(m_pActiveTool!=NULL)
				m_pActiveTool->Active();
			return true;
		}
		return false;

	}


	void   xBaseToolManager::OnMouseMove(const  xVector2& point)
	{
		if(m_pActiveTool!=NULL)
			m_pActiveTool->OnMouseMove(point);

	}

	void   xBaseToolManager::OnLButtonDown(const  xVector2& point)
	{

		if(m_pActiveTool!=NULL)
			m_pActiveTool->OnLButtonDown(point);

	}

	void  xBaseToolManager::OnRButtonDown(const  xVector2& point)
	{

		if(m_pActiveTool!=NULL)
			m_pActiveTool->OnRButtonDown(point);
	}

	void  xBaseToolManager::OnLButtonUp(const  xVector2& point)
	{
		if(m_pActiveTool!=NULL)
			m_pActiveTool->OnLButtonUp(point);

	}

	void  xBaseToolManager::OnRButtonUp(const  xVector2& point)
	{
		if(m_pActiveTool!=NULL)
			m_pActiveTool->OnRButtonUp(point);

	}


	void  xBaseToolManager::SetCamera(xCamera* pcamera)
	{
         if(pcamera==NULL||m_pCamera==pcamera)
			 return ;

		 m_pCamera=pcamera;
		 xBaseToolVector::iterator it =m_ToolVector.begin();
		 for(;it!=m_ToolVector.end();++it)
		 {
			(*it)->SetCamera(pcamera);
		 }
		 return ;
	}

	xCamera*  xBaseToolManager::GetCamera() const 
	{
		return m_pCamera;
	}


	void  xBaseToolManager::AddSeleectObject(xRenderObject* pObject)
	{
		if(pObject==NULL)
			return ;
		xRenderObjectVector::iterator it = std::find(m_SelectObjects.begin(),m_SelectObjects.end(),pObject);
		if(it==m_SelectObjects.end())
		{
			m_SelectObjects.push_back(pObject);
			pObject->EnableRenderBox(true);
		}
		return ;
	}

	void xBaseToolManager::ClearSelectObject()
	{
			xRenderObjectVector::iterator it = m_SelectObjects.begin();
			xRenderObjectVector::iterator itend = m_SelectObjects.end();
			for(;it!=itend;++it)
			{
				(*it)->EnableRenderBox(false);
			}
			m_SelectObjects.clear();

	}


	void xBaseToolManager::MoveSeletObjects(const  xVector3& pos )
	{
		if(m_SelectObjects.empty())
			return;

		xRenderObjectVector::iterator it = m_SelectObjects.begin();
		xRenderObjectVector::iterator itend = m_SelectObjects.end();
		for(;it!=itend;++it)
		{
			xSceneNode* pNode =  (*it)->GetAttachSceneNode();
			if(pNode==NULL)
				continue;
			xVector3 oldpos = pNode	->GetWorldPosition();
			oldpos+=pos;
			pNode->SetWorldPosition(oldpos);
		}

	}



	void  xBaseToolManager::RotationObjects(const xVector3& aixs, float angele)
	{
		if(m_SelectObjects.empty())
			return;

		xRenderObjectVector::iterator it = m_SelectObjects.begin();
		xRenderObjectVector::iterator itend = m_SelectObjects.end();
		for(;it!=itend;++it)
		{
			xSceneNode* pNode =  (*it)->GetAttachSceneNode();
			if(pNode==NULL)
				continue;
		    pNode->Rotation(aixs,angele);
		}
		return ;

	}


	xVector3 xBaseToolManager::GetSelectObjectsPos()const
	{

		if(m_SelectObjects.empty())
			return  xVector3(0.0f,0.0f,0.0f);

		xRenderObjectVector::const_iterator it = m_SelectObjects.begin();
		xRenderObjectVector::const_iterator itend = m_SelectObjects.end();
		int i  = 0;
		xVector3  pos(0.0f,0.0f,0.0f);
		for(;it!=itend;++it,++i)
		{
			xSceneNode* pNode =  (*it)->GetAttachSceneNode();
			if(pNode==NULL)
				continue;
			pos+= pNode	->GetWorldPosition();
		}

		return pos/=i;


	}





}